﻿using System;
using System.Collections.Generic;
using System.Text;
using EasyStudy.tools;

namespace EasyStudy
{
    public class Configuration
    {
        private string dbFileName;
        private bool isSoundOn;
        private IniFiles iniFiles;
        private static bool hasInstance = false;
        private static Configuration instance = null;
        private bool isIgnoreWeekend;
        private string backupDir;

        public bool IsIgnoreWeekend {
            get { return isIgnoreWeekend; }
        }

        public string BackupDir {
            get { return backupDir; }
        }

        public string DbFileName 
        {
            get { return dbFileName; }
            set { dbFileName = value; }
        }

        public static Configuration GetInstance() {
            if (hasInstance)
            {
                return instance;
            }
            else {
                instance = new Configuration();
                hasInstance = true;
                return instance;
            }
        }

        private Configuration() {
            string exePath = System.AppDomain.CurrentDomain.BaseDirectory;
            iniFiles = new IniFiles(exePath + "config.ini");
            isSoundOn = true;
            loadSetting();
        }

        public void setSoundsEnable(bool enable)
        {
            if (enable)
            {
                iniFiles.WriteString("SysSetting", "sound", "on");
            }
            else
            {
                iniFiles.WriteString("SysSetting", "sound", "off");
            }
        }

        private Boolean isAutoIgnoreWeekend() {
            string ignoreWeekendStr = iniFiles.ReadString("SysSetting", "ignore_weekend", "off");

            if (ignoreWeekendStr == "off")
            {
                return false;
            }
            else {
                return true;
            }
        }

        public void loadSetting() {
            dbFileName = iniFiles.ReadString("DB", "file.name", "EbbDataBase.db");
            string soundOnStr = iniFiles.ReadString("SysSetting", "sound", "on");
            string title = iniFiles.ReadString("SysSetting", "title", "Default");
            backupDir = iniFiles.ReadString("SysSetting", "backup_dir", System.AppDomain.CurrentDomain.BaseDirectory);
            isIgnoreWeekend = isAutoIgnoreWeekend();
            if (soundOnStr.ToUpper() == "ON")
            {
                GlobalSettings.SoundOn = true;
            }
            else
            {
                GlobalSettings.SoundOn = false;
            }
            GlobalSettings.title = title;
            
        }
    }
}
